

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="文章内容全部转载自 简书 博主  【Carson带你学安卓】 侵删. 转载地址：https:&#x2F;&#x2F;www.jianshu.com&#x2F;p&#x2F;ee1342fcf5e7@[toc] 前言 多线程的应用在Android开发中是非常常见的，常用方法主要有：   继承Thread类 实现Runnable接口 Handler AsyncTask HandlerThread  一、AsyncTask 定义 一个Andr">
<meta property="og:type" content="article">
<meta property="og:title" content="Android _ 多线程 _ AsyncTask类 概述及其使用">
<meta property="og:url" content="http://example.com/2021/12/16/Android%20_%20%E5%A4%9A%E7%BA%BF%E7%A8%8B%20_%20AsyncTask%E7%B1%BB%20%E6%A6%82%E8%BF%B0%E5%8F%8A%E5%85%B6%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="文章内容全部转载自 简书 博主  【Carson带你学安卓】 侵删. 转载地址：https:&#x2F;&#x2F;www.jianshu.com&#x2F;p&#x2F;ee1342fcf5e7@[toc] 前言 多线程的应用在Android开发中是非常常见的，常用方法主要有：   继承Thread类 实现Runnable接口 Handler AsyncTask HandlerThread  一、AsyncTask 定义 一个Andr">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/80bd6e55c778407f8fe7e4894ef211e0.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/117e33dd4dc7457a9a99260b5544aedb.png#pic_center">
<meta property="og:image" content="https://img-blog.csdnimg.cn/8fa7a00fd1dc4aa899857d95555043d7.gif#pic_center">
<meta property="article:published_time" content="2021-12-16T11:36:16.000Z">
<meta property="article:modified_time" content="2022-08-22T15:42:06.698Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/80bd6e55c778407f8fe7e4894ef211e0.png#pic_center">
  
  
  
  <title>Android _ 多线程 _ AsyncTask类 概述及其使用 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="Android _ 多线程 _ AsyncTask类 概述及其使用"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2021-12-16 19:36" pubdate>
          2021年12月16日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          59 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Android _ 多线程 _ AsyncTask类 概述及其使用</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>文章内容全部转载自 <strong>简书</strong> 博主  <strong>【Carson带你学安卓】</strong> 侵删.</p>
<p>转载地址：<a target="_blank" rel="noopener" href="https://www.jianshu.com/p/ee1342fcf5e7">https://www.jianshu.com/p/ee1342fcf5e7</a><br>@[toc]</p>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><ul>
<li>多线程的应用在Android开发中是非常常见的，常用方法主要有：</li>
</ul>
<ol>
<li>继承Thread类</li>
<li>实现Runnable接口</li>
<li>Handler</li>
<li>AsyncTask</li>
<li>HandlerThread</li>
</ol>
<h1 id="一、AsyncTask-定义"><a href="#一、AsyncTask-定义" class="headerlink" title="一、AsyncTask 定义"></a>一、AsyncTask 定义</h1><hr>
<p>一个Android 已封装好的轻量级异步类。属于抽象类，即使用时需实现子类。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">AsyncTask</span>&lt;Params, Progress, Result&gt; &#123; <br> ... <br> &#125;<br></code></pre></td></tr></table></figure>

<h1 id="二、-AsyncTask-作用"><a href="#二、-AsyncTask-作用" class="headerlink" title="二、 AsyncTask 作用"></a>二、 AsyncTask 作用</h1><hr>
<ul>
<li><p>实现多线程：在工作线程中执行任务，如 耗时任务</p>
</li>
<li><p>异步通信、消息传递：实现工作线程 &amp; 主线程（UI线程）之间的通信，即：将工作线程的执行结果传递给主线程，从而在主线程中执行相关的UI操作，保证线程安全。</p>
</li>
</ul>
<h1 id="三、AsyncTask-优点"><a href="#三、AsyncTask-优点" class="headerlink" title="三、AsyncTask 优点"></a>三、AsyncTask 优点</h1><hr>
<ul>
<li><p>方便实现异步通信<br>不需使用 “任务线程（如继承Thread类） + Handler”的复杂组合</p>
</li>
<li><p>节省资源<br>采用线程池的缓存线程 + 复用线程，避免了频繁创建 &amp; 销毁线程所带来的系统资源开销</p>
</li>
</ul>
<h1 id="四、AsyncTask-相关类与方法的介绍"><a href="#四、AsyncTask-相关类与方法的介绍" class="headerlink" title="四、AsyncTask 相关类与方法的介绍"></a>四、AsyncTask 相关类与方法的介绍</h1><hr>
<h2 id="4-1-类定义"><a href="#4-1-类定义" class="headerlink" title="4.1 类定义"></a>4.1 类定义</h2><p>AsyncTask类属于抽象类，即使用时需 实现子类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">abstract</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">AsyncTask</span>&lt;Params, Progress, Result&gt; &#123; <br> ... <br>&#125;<br><br><span class="hljs-comment">// 类中参数为3种泛型类型</span><br><span class="hljs-comment">// 整体作用：控制AsyncTask子类执行线程任务时各个阶段的返回类型</span><br><span class="hljs-comment">// 具体说明：</span><br>    <span class="hljs-comment">// a. Params：开始异步任务执行时传入的参数类型，对应excute（）中传递的参数</span><br>    <span class="hljs-comment">// b. Progress：异步任务执行过程中，返回下载进度值的类型</span><br>    <span class="hljs-comment">// c. Result：异步任务执行完成后，返回的结果类型，与doInBackground()的返回值类型保持一致</span><br><span class="hljs-comment">// 注：</span><br>    <span class="hljs-comment">// a. 使用时并不是所有类型都被使用</span><br>    <span class="hljs-comment">// b. 若无被使用，可用java.lang.Void类型代替</span><br>    <span class="hljs-comment">// c. 若有不同业务，需额外再写1个AsyncTask的子类</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h2 id="4-2-核心方法"><a href="#4-2-核心方法" class="headerlink" title="4.2 核心方法"></a>4.2 核心方法</h2><p><img src="https://img-blog.csdnimg.cn/80bd6e55c778407f8fe7e4894ef211e0.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<p>常用的方法执行顺序如下：<br><img src="https://img-blog.csdnimg.cn/117e33dd4dc7457a9a99260b5544aedb.png#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="五、AsyncTask-使用步骤"><a href="#五、AsyncTask-使用步骤" class="headerlink" title="五、AsyncTask 使用步骤"></a>五、AsyncTask 使用步骤</h1><ul>
<li>AsyncTask 使用步骤有3个</li>
</ul>
<ol>
<li>创建 <code>AsyncTask</code> 子类 &amp; 根据需求实现核心方法</li>
<li>创建 <code>AsyncTask</code> 子类的实例方法 （即 任务实例）</li>
<li>手动调用 <code>execute()</code> 从而执行异步线程任务</li>
</ol>
<ul>
<li>具体介绍如下：</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">/**</span><br><span class="hljs-comment">  * 步骤1：创建AsyncTask子类</span><br><span class="hljs-comment">  * 注： </span><br><span class="hljs-comment">  *   a. 继承AsyncTask类</span><br><span class="hljs-comment">  *   b. 为3个泛型参数指定类型；若不使用，可用java.lang.Void类型代替</span><br><span class="hljs-comment">  *   c. 根据需求，在AsyncTask子类内实现核心方法</span><br><span class="hljs-comment">  */</span><br><br>  <span class="hljs-keyword">private</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyTask</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AsyncTask</span>&lt;Params, Progress, Result&gt; &#123;<br><br>        ....<br><br>      <span class="hljs-comment">// 方法1：onPreExecute（）</span><br>      <span class="hljs-comment">// 作用：执行 线程任务前的操作</span><br>      <span class="hljs-comment">// 注：根据需求复写</span><br>      <span class="hljs-meta">@Override</span><br>      <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onPreExecute</span><span class="hljs-params">()</span> &#123;<br>           ...<br>        &#125;<br><br>      <span class="hljs-comment">// 方法2：doInBackground（）</span><br>      <span class="hljs-comment">// 作用：接收输入参数、执行任务中的耗时操作、返回 线程任务执行的结果</span><br>      <span class="hljs-comment">// 注：必须复写，从而自定义线程任务</span><br>      <span class="hljs-meta">@Override</span><br>      <span class="hljs-keyword">protected</span> String <span class="hljs-title function_">doInBackground</span><span class="hljs-params">(String... params)</span> &#123;<br><br>            ...<span class="hljs-comment">// 自定义的线程任务</span><br><br>            <span class="hljs-comment">// 可调用publishProgress（）显示进度, 之后将执行onProgressUpdate（）</span><br>             publishProgress(count);<br>              <br>         &#125;<br><br>      <span class="hljs-comment">// 方法3：onProgressUpdate（）</span><br>      <span class="hljs-comment">// 作用：在主线程 显示线程任务执行的进度</span><br>      <span class="hljs-comment">// 注：根据需求复写</span><br>      <span class="hljs-meta">@Override</span><br>      <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onProgressUpdate</span><span class="hljs-params">(Integer... progresses)</span> &#123;<br>            ...<br><br>        &#125;<br><br>      <span class="hljs-comment">// 方法4：onPostExecute（）</span><br>      <span class="hljs-comment">// 作用：接收线程任务执行结果、将执行结果显示到UI组件</span><br>      <span class="hljs-comment">// 注：必须复写，从而自定义UI操作</span><br>      <span class="hljs-meta">@Override</span><br>      <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onPostExecute</span><span class="hljs-params">(String result)</span> &#123;<br><br>         ...<span class="hljs-comment">// UI操作</span><br><br>        &#125;<br><br>      <span class="hljs-comment">// 方法5：onCancelled()</span><br>      <span class="hljs-comment">// 作用：将异步任务设置为：取消状态</span><br>      <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCancelled</span><span class="hljs-params">()</span> &#123;<br>        ...<br>        &#125;<br>  &#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment">  * 步骤2：创建AsyncTask子类的实例对象（即 任务实例）</span><br><span class="hljs-comment">  * 注：AsyncTask子类的实例必须在UI线程中创建</span><br><span class="hljs-comment">  */</span><br>  <span class="hljs-type">MyTask</span> <span class="hljs-variable">mTask</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyTask</span>();<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment">  * 步骤3：手动调用execute(Params... params) 从而执行异步线程任务</span><br><span class="hljs-comment">  * 注：</span><br><span class="hljs-comment">  *    a. 必须在UI线程中调用</span><br><span class="hljs-comment">  *    b. 同一个AsyncTask实例对象只能执行1次，若执行第2次将会抛出异常</span><br><span class="hljs-comment">  *    c. 执行任务中，系统会自动调用AsyncTask的一系列方法：onPreExecute() 、doInBackground()、onProgressUpdate() 、onPostExecute() </span><br><span class="hljs-comment">  *    d. 不能手动调用上述方法</span><br><span class="hljs-comment">  */</span><br>  mTask.execute()；<br></code></pre></td></tr></table></figure>

<h1 id="六、AsyncTask-实例讲解"><a href="#六、AsyncTask-实例讲解" class="headerlink" title="六、AsyncTask 实例讲解"></a>六、AsyncTask 实例讲解</h1><hr>
<h2 id="6-1-实例说明"><a href="#6-1-实例说明" class="headerlink" title="6.1  实例说明"></a>6.1  实例说明</h2><ol>
<li>点击按钮 则 开启线程执行线程任务</li>
<li>显示后台加载进度</li>
<li>加载完毕后更新UI组件</li>
<li>期间若点击取消按钮，则取消加载<br>实现效果图：<br><img src="https://img-blog.csdnimg.cn/8fa7a00fd1dc4aa899857d95555043d7.gif#pic_center" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></li>
</ol>
<h2 id="6-2-具体实现"><a href="#6-2-具体实现" class="headerlink" title="6.2 具体实现"></a>6.2 具体实现</h2><h3 id="6-2-1-布局文件-activity-main-xml"><a href="#6-2-1-布局文件-activity-main-xml" class="headerlink" title="6.2.1 布局文件 activity_main.xml"></a>6.2.1 布局文件 activity_main.xml</h3><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-meta">&lt;?xml version=<span class="hljs-string">&quot;1.0&quot;</span> encoding=<span class="hljs-string">&quot;utf-8&quot;</span>?&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">RelativeLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">&quot;http://schemas.android.com/apk/res/android&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">xmlns:tools</span>=<span class="hljs-string">&quot;http://schemas.android.com/tools&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;match_parent&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">android:gravity</span>=<span class="hljs-string">&quot;center&quot;</span></span><br><span class="hljs-tag">    <span class="hljs-attr">tools:context</span>=<span class="hljs-string">&quot;com.example.carson_ho.handler_learning.MainActivity&quot;</span>&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">Button</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_centerInParent</span>=<span class="hljs-string">&quot;true&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/button&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;点我加载&quot;</span>/&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">TextView</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/text&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_below</span>=<span class="hljs-string">&quot;@+id/button&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_centerInParent</span>=<span class="hljs-string">&quot;true&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;还没开始加载!&quot;</span> /&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">ProgressBar</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_below</span>=<span class="hljs-string">&quot;@+id/text&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/progress_bar&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;fill_parent&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:progress</span>=<span class="hljs-string">&quot;0&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:max</span>=<span class="hljs-string">&quot;100&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">style</span>=<span class="hljs-string">&quot;?android:attr/progressBarStyleHorizontal&quot;</span>/&gt;</span><br><br>    <span class="hljs-tag">&lt;<span class="hljs-name">Button</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_below</span>=<span class="hljs-string">&quot;@+id/progress_bar&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_centerInParent</span>=<span class="hljs-string">&quot;true&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:id</span>=<span class="hljs-string">&quot;@+id/cancel&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">&quot;wrap_content&quot;</span></span><br><span class="hljs-tag">        <span class="hljs-attr">android:text</span>=<span class="hljs-string">&quot;cancel&quot;</span>/&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">RelativeLayout</span>&gt;</span><br></code></pre></td></tr></table></figure>
<h3 id="6-2-2-主逻辑代码文件-MainActivity"><a href="#6-2-2-主逻辑代码文件-MainActivity" class="headerlink" title="6.2.2 主逻辑代码文件 MainActivity"></a>6.2.2 主逻辑代码文件 MainActivity</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> &#123;<br><br>    <span class="hljs-comment">// 线程变量</span><br>    MyTask mTask;<br><br>    <span class="hljs-comment">// 主布局中的UI组件</span><br>    Button button,cancel; <span class="hljs-comment">// 加载、取消按钮</span><br>    TextView text; <span class="hljs-comment">// 更新的UI组件</span><br>    ProgressBar progressBar; <span class="hljs-comment">// 进度条</span><br>    <br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 步骤1：创建AsyncTask子类</span><br><span class="hljs-comment">     * 注：</span><br><span class="hljs-comment">     *   a. 继承AsyncTask类</span><br><span class="hljs-comment">     *   b. 为3个泛型参数指定类型；若不使用，可用java.lang.Void类型代替</span><br><span class="hljs-comment">     *      此处指定为：输入参数 = String类型、执行进度 = Integer类型、执行结果 = String类型</span><br><span class="hljs-comment">     *   c. 根据需求，在AsyncTask子类内实现核心方法</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyTask</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AsyncTask</span>&lt;String, Integer, String&gt; &#123;<br><br>        <span class="hljs-comment">// 方法1：onPreExecute（）</span><br>        <span class="hljs-comment">// 作用：执行 线程任务前的操作</span><br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onPreExecute</span><span class="hljs-params">()</span> &#123;<br>            text.setText(<span class="hljs-string">&quot;加载中&quot;</span>);<br>            <span class="hljs-comment">// 执行前显示提示</span><br>        &#125;<br><br><br>        <span class="hljs-comment">// 方法2：doInBackground（）</span><br>        <span class="hljs-comment">// 作用：接收输入参数、执行任务中的耗时操作、返回 线程任务执行的结果</span><br>        <span class="hljs-comment">// 此处通过计算从而模拟“加载进度”的情况</span><br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">protected</span> String <span class="hljs-title function_">doInBackground</span><span class="hljs-params">(String... params)</span> &#123;<br><br>            <span class="hljs-keyword">try</span> &#123;<br>                <span class="hljs-type">int</span> <span class="hljs-variable">count</span> <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;<br>                <span class="hljs-type">int</span> <span class="hljs-variable">length</span> <span class="hljs-operator">=</span> <span class="hljs-number">1</span>;<br>                <span class="hljs-keyword">while</span> (count&lt;<span class="hljs-number">99</span>) &#123;<br><br>                    count += length;<br>                    <span class="hljs-comment">// 可调用publishProgress（）显示进度, 之后将执行onProgressUpdate（）</span><br>                    publishProgress(count);<br>                    <span class="hljs-comment">// 模拟耗时任务</span><br>                    Thread.sleep(<span class="hljs-number">50</span>);<br>                &#125;<br>            &#125;<span class="hljs-keyword">catch</span> (InterruptedException e) &#123;<br>                e.printStackTrace();<br>            &#125;<br><br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;<br>        &#125;<br><br>        <span class="hljs-comment">// 方法3：onProgressUpdate（）</span><br>        <span class="hljs-comment">// 作用：在主线程 显示线程任务执行的进度</span><br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onProgressUpdate</span><span class="hljs-params">(Integer... progresses)</span> &#123;<br><br>            progressBar.setProgress(progresses[<span class="hljs-number">0</span>]);<br>            text.setText(<span class="hljs-string">&quot;loading...&quot;</span> + progresses[<span class="hljs-number">0</span>] + <span class="hljs-string">&quot;%&quot;</span>);<br><br>        &#125;<br><br>        <span class="hljs-comment">// 方法4：onPostExecute（）</span><br>        <span class="hljs-comment">// 作用：接收线程任务执行结果、将执行结果显示到UI组件</span><br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onPostExecute</span><span class="hljs-params">(String result)</span> &#123;<br>            <span class="hljs-comment">// 执行完毕后，则更新UI</span><br>            text.setText(<span class="hljs-string">&quot;加载完毕&quot;</span>);<br>        &#125;<br><br>        <span class="hljs-comment">// 方法5：onCancelled()</span><br>        <span class="hljs-comment">// 作用：将异步任务设置为：取消状态</span><br>        <span class="hljs-meta">@Override</span><br>        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCancelled</span><span class="hljs-params">()</span> &#123;<br><br>            text.setText(<span class="hljs-string">&quot;已取消&quot;</span>);<br>            progressBar.setProgress(<span class="hljs-number">0</span>);<br><br>        &#125;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> &#123;<br>        <span class="hljs-built_in">super</span>.onCreate(savedInstanceState);<br><br>        <span class="hljs-comment">// 绑定UI组件</span><br>        setContentView(R.layout.activity_main);<br><br>        button = (Button) findViewById(R.id.button);<br>        cancel = (Button) findViewById(R.id.cancel);<br>        text = (TextView) findViewById(R.id.text);<br>        progressBar = (ProgressBar) findViewById(R.id.progress_bar);<br><br>        <span class="hljs-comment">/**</span><br><span class="hljs-comment">         * 步骤2：创建AsyncTask子类的实例对象（即 任务实例）</span><br><span class="hljs-comment">         * 注：AsyncTask子类的实例必须在UI线程中创建</span><br><span class="hljs-comment">         */</span><br>        mTask = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyTask</span>();<br><br>        <span class="hljs-comment">// 加载按钮按按下时，则启动AsyncTask</span><br>        <span class="hljs-comment">// 任务完成后更新TextView的文本</span><br>        button.setOnClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">View</span>.OnClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onClick</span><span class="hljs-params">(View v)</span> &#123;<br><br>                <span class="hljs-comment">/**</span><br><span class="hljs-comment">                 * 步骤3：手动调用execute(Params... params) 从而执行异步线程任务</span><br><span class="hljs-comment">                 * 注：</span><br><span class="hljs-comment">                 *    a. 必须在UI线程中调用</span><br><span class="hljs-comment">                 *    b. 同一个AsyncTask实例对象只能执行1次，若执行第2次将会抛出异常</span><br><span class="hljs-comment">                 *    c. 执行任务中，系统会自动调用AsyncTask的一系列方法：onPreExecute() 、doInBackground()、onProgressUpdate() 、onPostExecute()</span><br><span class="hljs-comment">                 *    d. 不能手动调用上述方法</span><br><span class="hljs-comment">                 */</span><br>                mTask.execute();<br>            &#125;<br>        &#125;);<br><br>        cancel = (Button) findViewById(R.id.cancel);<br>        cancel.setOnClickListener(<span class="hljs-keyword">new</span> <span class="hljs-title class_">View</span>.OnClickListener() &#123;<br>            <span class="hljs-meta">@Override</span><br>            <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onClick</span><span class="hljs-params">(View v)</span> &#123;<br>                <span class="hljs-comment">// 取消一个正在执行的任务,onCancelled方法将会被调用</span><br>                mTask.cancel(<span class="hljs-literal">true</span>);<br>            &#125;<br>        &#125;);<br><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="七、-使用-AsyncTask-时的注意点"><a href="#七、-使用-AsyncTask-时的注意点" class="headerlink" title="七、 使用 AsyncTask 时的注意点"></a>七、 使用 AsyncTask 时的注意点</h1><hr>
<h2 id="7-1-关于生命周期"><a href="#7-1-关于生命周期" class="headerlink" title="7.1 关于生命周期"></a>7.1 关于生命周期</h2><ul>
<li>结论<br><code>AsyncTask</code> 不与任何组件绑定生命周期</li>
<li>使用建议<br>在 <code>Activity</code> 或 <code>Fragment</code> 中使用 <code>AsyncTask</code> 时</li>
</ul>
<p>最好在 <code>Activity</code>或<code>Fragment</code>的<code>onDestroy()</code> 调用 <code>cancel(boolean)</code>;</p>
<h2 id="7-2-关于内存泄露"><a href="#7-2-关于内存泄露" class="headerlink" title="7.2 关于内存泄露"></a>7.2 关于内存泄露</h2><ul>
<li><p>结论<br>当 <code>Activity </code> 重新创建时 （屏幕旋转 或 <code>Activity</code> 被意外销毁时后恢复），之前运行的 <code>AsyncTask</code> （非静态的内部类) 持有的之前 <code>Activity</code> 引用已无效，故复写的<code>onPostExecute()</code> 将不生效，即无法更新UI操作。</p>
</li>
<li><p>使用建议<br>在 <code>Activity </code> 恢复时的对应方法<strong>重启</strong>任务线程</p>
</li>
</ul>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%AE%89%E5%8D%93/" class="category-chain-item">安卓</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Android/">#Android</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Android _ 多线程 _ AsyncTask类 概述及其使用</div>
      <div>http://example.com/2021/12/16/Android _ 多线程 _ AsyncTask类 概述及其使用/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2021年12月16日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/12/19/Web%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20_%20%E8%B4%AD%E7%89%A9%E7%B3%BB%E7%BB%9Fv1.0%20_%20%E5%BC%80%E5%8F%91%E8%AE%B0%E5%BD%95%EF%BC%88%E4%B8%80%EF%BC%89%20_%20%E5%A4%A7%E5%AD%A6%E7%94%9F%E9%97%B2%E7%BD%AE%E7%89%A9%E5%93%81%E4%BA%A4%E6%98%93%E7%B3%BB%E7%BB%9F%20_%20%E9%80%89%E6%8B%A9%E9%A1%B5%E9%9D%A2%E6%A8%A1%E6%9D%BF%EF%BC%88%E9%99%84%E8%B5%84%E6%BA%90%EF%BC%89%EF%BC%8C%E4%BD%BF%E7%94%A8%20JQuery%20AJAX%E5%AE%9E%E7%8E%B0%E6%B3%A8%E5%86%8C%E3%80%81%E7%99%BB%E9%99%86/" title="Web项目实战 _ 购物系统v1.0 _ 开发记录（一） _ 大学生闲置物品交易系统 _ 选择页面模板（附资源），使用 JQuery AJAX实现注册、登陆">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Web项目实战 _ 购物系统v1.0 _ 开发记录（一） _ 大学生闲置物品交易系统 _ 选择页面模板（附资源），使用 JQuery AJAX实现注册、登陆</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/12/14/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%B7%A5%E7%A8%8B%E5%AE%9E%E8%B7%B5v1.0%E7%89%88%E6%9C%AC%20_%20%EF%BC%88%E4%B8%80%EF%BC%89%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA%20(1)%20_%20%E7%BB%9F%E4%B8%80%E9%85%8D%E7%BD%AEhadoop3.1.3%E5%AE%8C%E5%85%A8%E5%88%86%E5%B8%83%E5%BC%8F%E9%9B%86%E7%BE%A4%E4%B8%89%E4%B8%AA%E8%8A%82%E7%82%B9/" title="大数据工程实践v1.0版本 _ （一）环境搭建 (1) _ 统一配置hadoop3.1.3完全分布式集群三个节点">
                        <span class="hidden-mobile">大数据工程实践v1.0版本 _ （一）环境搭建 (1) _ 统一配置hadoop3.1.3完全分布式集群三个节点</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
